//
//  规格筛选Field.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/11/7.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay
import soapp

private let selectedColor = ___shop.tint
private let normalColor = UIColor.color(light: "#F2F2F2", dark: "#aaaaaa22")
private let normalTextColor = UIColor.color(light: "#666", dark: "#aaa")


private let insets:CGFloat = 12
private let lineSpace:CGFloat = 12

class GgFixTextKit: FOptionV {
    
    override func setOptionLayout() {
        self.edeg = UIEdgeInsets(top: 2, left: 6, bottom: 2, right: 6)
        eelay = [
            [imgV,[ee.T.L.B.R]],
            [titleL,[ee.T.L.B.R],"30"],
            [bt,[ee.T.L.B.R]],
        ]
        bt.setContentConstrainToLowLever()
        imgV.setImage(nil, for: .normal)
        imgV.setImage(nil, for: .selected)        
        self.bsui.set(radius: (1), width: nil, color: nil)
        _ = titleL.ui.center.font13.text(color: .color(light: "#666", dark: "#aaa"))
    }
    open override var intrinsicContentSize: CGSize
    {
        //        let s_w = self.edeg.right + self.titleL.intrinsicContentSize.width + self.edeg.left
        
        let que_num = 4
        let s_w = (Swidth - 2*AppTheme.edge - (que_num-1).cg_floatValue*insets)/que_num.cg_floatValue
        let s_h = self.edeg.top + self.edeg.bottom + self.titleL.intrinsicContentSize.height
        return [s_w, s_h]
    }
    
    override func mo_model_reload() {
        
        self.titleL.text = mo.model["name",""]
        if mo.model[FK.selected,"NO"] == "YES"
        {
            imgV.bsui.background(hex: "#fff")
            imgV.isSelected =  true
            _ = titleL.ui.font13.cl_222
            self.bsui.set(radius: 0, width: 1, color: selectedColor)
        }
        else
        {
            imgV.isSelected = false
            imgV.bsui.background(hex: "#fff")
            _ = titleL.ui.font13.cl_222
            self.bsui.set(radius: 0, width: 1, color: normalColor)
        }
        backgroundColor = .clear
        bt.setImage(nil, for: .normal)
        bt.setImage(nil, for: .selected)
    }
}


public class GgStrechTextKit: FOptionV {
    
    public override func setOptionLayout() {
        self.edeg = UIEdgeInsets(top: 6, left: 12, bottom: 6, right: 12)
        eelay = [
            [imgV,[ee.T.L.B.R]],
            [titleL,[ee.T.L.B.R,[edeg.top,edeg.left,-edeg.bottom,-edeg.right]],"22"],
            [bt,[ee.T.L.B.R]],
        ]
        titleL.setContentConstrainToLowLever()
        bt.setContentConstrainToLowLever()
        imgV.setImage(nil, for: .normal)
        imgV.setImage(nil, for: .selected)
        
        
        self.bsui.set(radius: (1), width: nil, color: nil)
        _ = titleL.ui.center.font13.cl_666
    }
    open override var intrinsicContentSize: CGSize
    {
        var s_w = self.edeg.right + self.titleL.intrinsicContentSize.width + self.edeg.left
        let s_h = self.edeg.top + self.edeg.bottom + self.titleL.intrinsicContentSize.height
        if s_w > (Swidth-2*AppTheme.edge)
        {
            s_w = Swidth-2*AppTheme.edge
        }
        return [s_w, s_h]
    }
    
    public override func mo_model_reload() {
        
        var name = mo.model["title",""]
        if name.len < 1
        {
            name = mo.model["name",""]
        }
        self.titleL.text = name
        if mo.model[FK.selected,"NO"] == "YES"
        {
            imgV.bsui.background(color: selectedColor)
            imgV.isSelected =  true
            _ = titleL.ui.font13.cl_fff
            self.bsui.background(color: selectedColor)
            self.bsui.set(radius: 4, width: 1, color: selectedColor)
        }
        else
        {
            imgV.isSelected = false
            imgV.bsui.background(color: normalColor)
            _ = titleL.ui.font13.text(color: .color(light: "#666", dark: "#aaa"))
            self.bsui.set(radius: 4, width: 1, color: normalColor)
        }
        backgroundColor = .clear
        bt.setImage(nil, for: .normal)
        bt.setImage(nil, for: .selected)
    }
}

class GgIconKit: FOptionV {
    
    let iconV = UIImageView()
    let imgBk = UIView()

    override func setOptionLayout() {
        eelay = [
            [imgBk,"\(60.co)",60.co,[ee.T.X]],
            [titleL,[ee.L.B.R]],
        ]
        
        self.edeg = UIEdgeInsets(top: 2, left: 6, bottom: 2, right: 6)
        imgBk.eelay = [
            [iconV,[ee.T.L.R,[4,4,-4]]],
        ]
        let scale = NSLayoutConstraint(item: iconV, attribute: .height, relatedBy: .equal, toItem: iconV, attribute: .width, multiplier: 1.0, constant: 0)
        iconV.addConstraint(scale)
        bt.setContentConstrainToLowLever()
        imgV.setImage(nil, for: .normal)
        imgV.setImage(nil, for: .selected)
        iconV.setContentConstrainToLowLever()
        
        self.bsui.set(radius: (1), width: nil, color: nil)
        _ = titleL.ui.center.font11.text(color: normalTextColor)
//        iconV.img_url = __heads.one_str
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        addSubview(bt)
        bt.frame = self.bounds
    }
    open override var intrinsicContentSize: CGSize
    {
        //        let s_w = self.edeg.right + self.titleL.intrinsicContentSize.width + self.edeg.left
        
//        let que_num = 6
//        let s_w = (Swidth - 2*AppTheme.edge - (que_num-1).cg_floatValue*insets)/que_num.cg_floatValue
//        let s_h = (Swidth - 2*AppTheme.edge)/6
        return [60.co,88.co]
    }
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        imgBk.bsui.set(radius: 0, width: 1, color: .color(light: "#999", dark: "#aaa"))
    }
    
    override func mo_model_reload() {
        titleL.text = mo.model["title",""]
        self.iconV.img_url = mo.model["thumb",__heads.one_str]
        if mo.model[FK.selected,"NO"] == "YES"
        {
            imgV.bsui.background(hex: "#fff")
            imgV.isSelected =  true
            _ = titleL.ui.font11.text(color: selectedColor)
            imgBk.bsui.set(radius: 0, width: 1, color: selectedColor)
        }
        else
        {
            imgV.isSelected = false
            imgV.bsui.background(color: ._cellBackground)
            _ = titleL.ui.font11.text(color: normalTextColor)
            imgBk.bsui.set(radius: 0, width: 1, color: normalTextColor)
        }
        backgroundColor = .clear
        bt.setImage(nil, for: .normal)
        bt.setImage(nil, for: .selected)
    }
}


class 规格选择Cell<Kit:FOptionV>: FieldCell {
    
    var field1 = TagsOptionField<Kit>()
    
    override func addLayoutRules() {
        titleL.ui.font13.text(color: ._foreground).node("title")
        let w = Int(FieldCell.name_width)
        //        let t = Int(FieldCell.top)
        
        contentView.eelay = [
            [titleL,[ee.T.L,[8,AppTheme.edge]],"21",.<w],
            [requiredMark,[titleL,ee.Y,3],[titleL,ee.R,ee.L,2]],
            [__line,[ee.L.B.R,[0,0,0]],"0.5"]
        ]
        
        __line.backgroundColor = ._separator
        self.indicator = .none
        titleL.setContentConstrainToHighLever()
        requiredMark.ui.font18.text("*").text(color: .clear)
        rightIcon.ui.font(size: 13).ncolor(color: .color(light: "#aaa", dark: "#8882"))
        contentView.eelay = [
            [field1,[ee.L.B,[AppTheme.edge,-18]],[titleL,ee.B,ee.T,16]],
        ]
        field1.tagsV.preferredMaxLayoutWidth = Swidth - 2*AppTheme.edge - 4
        field1.insets = insets
        field1.lineSpace = lineSpace
    
        contentView.backgroundColor = .clear
        self.backgroundColor = .clear
    }
    
    
    override func mo_model_reload() {
        titleL.text = mo.model["title",""]
        field1.controller = self.content_controller
        if let f = DataNode<NSMutableDictionary>.path(node: "\(FK.field)", mo.model)
        {
            field1.mo.model = f
        }
        else
        {
            if let f = DataNode<NSMutableDictionary>.path(node: "\(FK.fields).0", mo.model)
            {
                field1.mo.model = f
            }
        }
    }
    
}



